﻿using System.Collections.Generic;
using System.Threading.Tasks;
using System.Linq;
using Wasion.HHU.Data.Models;
using Wasion.HHU.Data.Repositories;
using SQLite.Net;
using System;
using System.Linq.Expressions;

namespace Wasion.HHU.Data.Repositories
{
    public class HHULogDbRepository : BaseDbRepository
    {
        public HHULogDbRepository() : base()
        {

        }
        public HHULogDbRepository(SQLiteConnection connection)
            : base(connection)
        {
        }

        public Task<List<HHULog>> GetAllList()
        {
            List<HHULog> hHULogs;

            lock (databaseLock)
            {
                hHULogs = DbConnection.Table<HHULog>().
                    OrderByDescending(x => x.ExecutionTime).
                    ToList();
            }
            return Task.FromResult(hHULogs);
        }

        public Task<List<HHULog>> GetList(Expression<Func<HHULog, bool>> filter)
        {
            List<HHULog> hHULogs;

            lock (databaseLock)
            {
                hHULogs = DbConnection.Table<HHULog>().
                    OrderByDescending(x => x.ExecutionTime).
                    Where(filter).
                    ToList();
            }
            return Task.FromResult(hHULogs);
        }

        public void Insert(HHULog hHULog)
        {
            lock (databaseLock)
            {
                DbConnection.CreateTable<HHULog>();
                DbConnection.Insert(hHULog);
            }
        }

        public void Update(HHULog hHULog)
        {
            lock (databaseLock)
            {
                DbConnection.Update(hHULog);
            }
        }

        public HHULog Find(object primaryKey)
        {
            HHULog hHULog;

            lock (databaseLock)
            {
                hHULog = DbConnection.Find<HHULog>(primaryKey);
            }
            return hHULog;
        }

        public void Delete(object primaryKey)
        {
            lock (databaseLock)
            {
                DbConnection.Delete<HHULog>(primaryKey);
            }
        }
    }
}
